﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;

namespace 数据迁移工具.Common
{
    public class ConsoleLog
    {
        private static Dictionary<string, Control> _dict = new Dictionary<string, Control>();

        public static void AddControl(string name,Control control)
        {
            if(!_dict.ContainsKey(name))
                _dict.Add(name, control);
            _dict[name] = control;
        }

        public static Control GetControl(string name)
        {
            if(_dict.TryGetValue(name, out Control control)) 
            {
                return control;
            }
            else
            {
                MessageBox.Show("找不到对应的控件，请检查是否初始化该控件");
                return null;
            }
        }

        public static void Info(string name,string message)
        {
            Output(name,message,Color.Black);
        }

        public static void Success(string name,string message)
        {
            Output(name, message, Color.LimeGreen);
        }

        public static void Warn(string name, string message)
        {
            Output(name, message, Color.Orange);
          
        }

        public static void Error(string name, string message)
        {
            Output(name, message, Color.DarkRed);
        }

        private static void Output(string name,string message,Color color)
        {
            if (string.IsNullOrEmpty(name))
                return;
            if (_dict.TryGetValue(name, out var control))
            {
                if (control is UIRichTextBox RhTextBox)
                {
                    RhTextBox.Invoke(() =>
                    {
                        RhTextBox.SelectionColor = color;
                        RhTextBox.AppendText(message + "\n");
                        RhTextBox.SelectionStart = RhTextBox.TextLength;
                        RhTextBox.ScrollToCaret();
                    });
                }
            }
            else
                return;
        }
    }
}
